<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico"/>
<title>DynamoRIO API: $title</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">DynamoRIO API
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.1.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('API_tutorial_bbdynsize5.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="contents">
<div class="textblock"><p>In this variation we use inlined atomic instructions to adjust the counters. On a single core box this form should be quite fast, but on a multi-core box the cache line locking for atomic operations will slow things dramatically. We'll look at forms more suitable for multi-core further on.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="dr__api_8h.html" title="Top-level include file for DynamoRIO API.">dr_api.h</a>&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifdef WINDOWS</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor"># define DISPLAY_STRING(msg) dr_messagebox(msg)</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#else</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor"># define DISPLAY_STRING(msg) dr_printf(&quot;%s\n&quot;, msg)</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor"></span></div>
<div class="line"><span class="keyword">typedef</span> <span class="keyword">struct </span>bb_counts {</div>
<div class="line">   uint64 blocks;</div>
<div class="line">   uint64 total_size;</div>
<div class="line">} bb_counts;</div>
<div class="line"></div>
<div class="line"><span class="keyword">static</span> bb_counts counts_as_built;</div>
<div class="line"><span class="keywordtype">void</span> *as_built_lock;</div>
<div class="line"></div>
<div class="line"><span class="keyword">static</span> bb_counts counts_dynamic;</div>
<div class="line"></div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span></div>
<div class="line">event_exit(<span class="keywordtype">void</span>);</div>
<div class="line"><span class="keyword">static</span> <a class="code" href="dr__events_8h.html#a689518ac5d1ad136b13e03012702b209">dr_emit_flags_t</a></div>
<div class="line">event_basic_block(<span class="keywordtype">void</span> *drcontext, <span class="keywordtype">void</span> *tag, instrlist_t *bb,</div>
<div class="line">                  <span class="keywordtype">bool</span> for_trace, <span class="keywordtype">bool</span> translating);</div>
<div class="line"></div>
<div class="line">DR_EXPORT <span class="keywordtype">void</span></div>
<div class="line"><a class="code" href="dr__api_8h.html#a20a4dc9da7f6bb9121e30bb3570c6961">dr_init</a>(<a class="code" href="dr__defines_8h.html#a68540a70b4f8150a4fe6dcec91bf8825">client_id_t</a> <span class="keywordtype">id</span>)</div>
<div class="line">{</div>
<div class="line">    <span class="comment">/* register events */</span></div>
<div class="line">    <a class="code" href="dr__events_8h.html#a985537df683007e1392e8a3b095ef363">dr_register_exit_event</a>(event_exit);</div>
<div class="line">    <a class="code" href="dr__events_8h.html#a043a3588c4cf0c2d6838140fce530e72">dr_register_bb_event</a>(event_basic_block);</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* initialize lock */</span></div>
<div class="line">    as_built_lock = <a class="code" href="dr__tools_8h.html#a55da3fd48670f25ec69ee5daf5975de5">dr_mutex_create</a>();</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span></div>
<div class="line">event_exit(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">    <span class="comment">/* Display results - we must first snpritnf the string as on windows</span></div>
<div class="line"><span class="comment">       dr_printf(), dr_messagebox() and dr_fprintf() can&#39;t print floats. */</span></div>
<div class="line">    <span class="keywordtype">char</span> msg[512];</div>
<div class="line">    <span class="keywordtype">int</span> len;</div>
<div class="line">    len = snprintf(msg, <span class="keyword">sizeof</span>(msg)/<span class="keyword">sizeof</span>(msg[0]),</div>
<div class="line">                   <span class="stringliteral">&quot;Number of blocks built : %&quot;</span>UINT64_FORMAT_CODE<span class="stringliteral">&quot;\n&quot;</span></div>
<div class="line">                   <span class="stringliteral">&quot;     Average size      : %5.2lf instructions\n&quot;</span></div>
<div class="line">                   <span class="stringliteral">&quot;Number of blocks executed  : %&quot;</span>UINT64_FORMAT_CODE<span class="stringliteral">&quot;\n&quot;</span></div>
<div class="line">                   <span class="stringliteral">&quot;     Average weighted size : %5.2lf instructions\n&quot;</span>,</div>
<div class="line">                   counts_as_built.blocks,</div>
<div class="line">                   counts_as_built.total_size / (<span class="keywordtype">double</span>)counts_as_built.blocks,</div>
<div class="line">                   counts_dynamic.blocks,</div>
<div class="line">                   counts_dynamic.total_size / (<span class="keywordtype">double</span>)counts_dynamic.blocks);</div>
<div class="line">    <a class="code" href="dr__tools_8h.html#a7c07630dffdfd92bc6ee63abf405768c">DR_ASSERT</a>(len &gt; 0);</div>
<div class="line">    msg[<span class="keyword">sizeof</span>(msg)/<span class="keyword">sizeof</span>(msg[0])-1] = <span class="charliteral">&#39;\0&#39;</span>;</div>
<div class="line">    DISPLAY_STRING(msg);</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* free mutex */</span></div>
<div class="line">    <a class="code" href="dr__tools_8h.html#aaab148b68bc8e1479a14c175cbf42fc0">dr_mutex_destroy</a>(as_built_lock);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keyword">static</span> <a class="code" href="dr__events_8h.html#a689518ac5d1ad136b13e03012702b209">dr_emit_flags_t</a></div>
<div class="line">event_basic_block(<span class="keywordtype">void</span> *drcontext, <span class="keywordtype">void</span> *tag, instrlist_t *bb,</div>
<div class="line">                  <span class="keywordtype">bool</span> for_trace, <span class="keywordtype">bool</span> translating)</div>
<div class="line">{</div>
<div class="line">    uint num_instructions = 0;</div>
<div class="line">    <a class="code" href="structinstr__t.html">instr_t</a> *instr, *where = NULL;</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* count the number of instructions in this block */</span></div>
<div class="line">    <span class="keywordflow">for</span> (instr = <a class="code" href="dr__ir__instrlist_8h.html#ac9a47c1b7376ca06d8dcb4b3094e5a67">instrlist_first</a>(bb); instr != NULL; instr = <a class="code" href="dr__ir__instr_8h.html#a9a11b5b0700f7601087e2fda87175040">instr_get_next</a>(instr)) {</div>
<div class="line">        num_instructions++;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* update the as-built counts */</span></div>
<div class="line">    <a class="code" href="dr__tools_8h.html#a04beaefb3e0be6bd0dac39f76398ec1e">dr_mutex_lock</a>(as_built_lock);</div>
<div class="line">    counts_as_built.blocks++;</div>
<div class="line">    counts_as_built.total_size += num_instructions;</div>
<div class="line">    <a class="code" href="dr__tools_8h.html#a61a3a4ffffd0058a37211405fbb6ba51">dr_mutex_unlock</a>(as_built_lock);</div>
<div class="line"></div>
<div class="line">+     <span class="comment">/* increment counters */</span></div>
<div class="line">+     where = <a class="code" href="dr__ir__instrlist_8h.html#ac9a47c1b7376ca06d8dcb4b3094e5a67">instrlist_first</a>(bb);</div>
<div class="line">+     <a class="code" href="dr__ir__utils_8h.html#a44636ca6809a9fb22cf817701de9ee09">dr_save_arith_flags</a>(drcontext, bb, where, SPILL_SLOT_1);</div>
<div class="line">+ #ifdef X86_32</div>
<div class="line">+     <span class="comment">/* Since the counters are 64-bit we must use an add + an addc to increment.</span></div>
<div class="line"><span class="comment">+      * The operations is still effectively atomic since we&#39;re only increasing</span></div>
<div class="line"><span class="comment">+      * the count. */</span></div>
<div class="line">+     <a class="code" href="dr__ir__utils_8h.html#a3b0b7e9e09a9a85ca1a707da3ded6f69">instrlist_meta_preinsert</a>(bb, where,</div>
<div class="line">+         <a class="code" href="dr__ir__macros_8h.html#a8104e895719c1f3a8558067b023cf9c5">LOCK</a>(<a class="code" href="dr__ir__macros_8h.html#a90f771585c3c11982b6c1604e249cdbf">INSTR_CREATE_add</a>(drcontext,</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a54b8bd667a8a940e94bfcc7842c958eb">OPND_CREATE_ABSMEM</a>((byte *)&amp;counts_dynamic.blocks, <a class="code" href="dr__ir__opnd_8h.html#aaf8fd5f0e57d456151c951e0f3715fc4a0ccef0d75b3f473bf275388804c8b85c">OPSZ_4</a>),</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a0cf77f656e96684678c30892c0c718af">OPND_CREATE_INT8</a>(1))));</div>
<div class="line">+     <a class="code" href="dr__ir__utils_8h.html#a3b0b7e9e09a9a85ca1a707da3ded6f69">instrlist_meta_preinsert</a>(bb, where,</div>
<div class="line">+         <a class="code" href="dr__ir__macros_8h.html#a8104e895719c1f3a8558067b023cf9c5">LOCK</a>(<a class="code" href="dr__ir__macros_8h.html#af637a7cf25e1961f3a96784c733e86c2">INSTR_CREATE_adc</a>(drcontext,</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a54b8bd667a8a940e94bfcc7842c958eb">OPND_CREATE_ABSMEM</a>((byte *)&amp;counts_dynamic.blocks + 4, <a class="code" href="dr__ir__opnd_8h.html#aaf8fd5f0e57d456151c951e0f3715fc4a0ccef0d75b3f473bf275388804c8b85c">OPSZ_4</a>),</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a0cf77f656e96684678c30892c0c718af">OPND_CREATE_INT8</a>(0))));</div>
<div class="line">+</div>
<div class="line">+     <a class="code" href="dr__ir__utils_8h.html#a3b0b7e9e09a9a85ca1a707da3ded6f69">instrlist_meta_preinsert</a>(bb, where,</div>
<div class="line">+         <a class="code" href="dr__ir__macros_8h.html#a8104e895719c1f3a8558067b023cf9c5">LOCK</a>(<a class="code" href="dr__ir__macros_8h.html#a90f771585c3c11982b6c1604e249cdbf">INSTR_CREATE_add</a>(drcontext,</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a54b8bd667a8a940e94bfcc7842c958eb">OPND_CREATE_ABSMEM</a>((byte *)&amp;counts_dynamic.total_size, <a class="code" href="dr__ir__opnd_8h.html#aaf8fd5f0e57d456151c951e0f3715fc4a0ccef0d75b3f473bf275388804c8b85c">OPSZ_4</a>),</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a39cadf7c35d28a1dedf9b945cf1aa656">OPND_CREATE_INT_32OR8</a>(num_instructions))));</div>
<div class="line">+     <a class="code" href="dr__ir__utils_8h.html#a3b0b7e9e09a9a85ca1a707da3ded6f69">instrlist_meta_preinsert</a>(bb, where,</div>
<div class="line">+         <a class="code" href="dr__ir__macros_8h.html#a8104e895719c1f3a8558067b023cf9c5">LOCK</a>(<a class="code" href="dr__ir__macros_8h.html#af637a7cf25e1961f3a96784c733e86c2">INSTR_CREATE_adc</a>(drcontext,</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a54b8bd667a8a940e94bfcc7842c958eb">OPND_CREATE_ABSMEM</a>((byte *)&amp;counts_dynamic.total_size + 4, <a class="code" href="dr__ir__opnd_8h.html#aaf8fd5f0e57d456151c951e0f3715fc4a0ccef0d75b3f473bf275388804c8b85c">OPSZ_4</a>),</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a0cf77f656e96684678c30892c0c718af">OPND_CREATE_INT8</a>(0))));</div>
<div class="line">+ #<span class="keywordflow">else</span> <span class="comment">/* X86_64 */</span></div>
<div class="line">+     <a class="code" href="dr__ir__utils_8h.html#a3b0b7e9e09a9a85ca1a707da3ded6f69">instrlist_meta_preinsert</a>(bb, where,</div>
<div class="line">+         <a class="code" href="dr__ir__macros_8h.html#a8104e895719c1f3a8558067b023cf9c5">LOCK</a>(<a class="code" href="dr__ir__macros_8h.html#a759b7342ed4080b9c12809d3b2e2718a">INSTR_CREATE_inc</a>(drcontext,</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a54b8bd667a8a940e94bfcc7842c958eb">OPND_CREATE_ABSMEM</a>((byte *)&amp;counts_dynamic.blocks, <a class="code" href="dr__ir__opnd_8h.html#aaf8fd5f0e57d456151c951e0f3715fc4a066a3d6699a61167eb0ba00e529c2e41">OPSZ_8</a>))));</div>
<div class="line">+     <a class="code" href="dr__ir__utils_8h.html#a3b0b7e9e09a9a85ca1a707da3ded6f69">instrlist_meta_preinsert</a>(bb, where,</div>
<div class="line">+         <a class="code" href="dr__ir__macros_8h.html#a8104e895719c1f3a8558067b023cf9c5">LOCK</a>(<a class="code" href="dr__ir__macros_8h.html#a90f771585c3c11982b6c1604e249cdbf">INSTR_CREATE_add</a>(drcontext,</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a54b8bd667a8a940e94bfcc7842c958eb">OPND_CREATE_ABSMEM</a>((byte *)&amp;counts_dynamic.total_size, <a class="code" href="dr__ir__opnd_8h.html#aaf8fd5f0e57d456151c951e0f3715fc4a066a3d6699a61167eb0ba00e529c2e41">OPSZ_8</a>),</div>
<div class="line">+                               <a class="code" href="dr__ir__macros_8h.html#a39cadf7c35d28a1dedf9b945cf1aa656">OPND_CREATE_INT_32OR8</a>(num_instructions))));</div>
<div class="line">+ #endif</div>
<div class="line">+     <a class="code" href="dr__ir__utils_8h.html#a563b0de37a51cb2e84337deef30a57cf">dr_restore_arith_flags</a>(drcontext, bb, where, SPILL_SLOT_1);</div>
<div class="line">+</div>
<div class="line">    <span class="keywordflow">return</span> <a class="code" href="dr__events_8h.html#a689518ac5d1ad136b13e03012702b209a7d25c4546544d1a8c0b77c69230772eb">DR_EMIT_DEFAULT</a>;</div>
<div class="line">}</div>
</div><!-- fragment --><p>[ <a class="el" href="API_tutorial_bbdynsize4.html">prev</a> | <a class="el" href="API_tutorial_bbdynsize6.html">next</a> ] </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer" style="float:none;text-align:center"><img border=0 src="favicon.png"> &nbsp;  DynamoRIO API version 5.0.0 --- Wed Sep 10 2014 21:36:44 &nbsp; <img border=0 src="favicon.png">
</small></address>
<!--END !GENERATE_TREEVIEW-->
</body>
</html>
